<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8" />
    <title>excel表头设置</title>
	<link rel="stylesheet" type="text/css" href="/static/basic/plugins/easyui/themes/bootstrap/easyui.css">
	<link href="/static/basic/plugins/fontawesome-free-5.15.4-web/css/all.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="/static/basic/plugins/webuploader/webuploader.css?v=12171">
	<link rel="stylesheet" type="text/css" href="/static/m.css" >
	
</head>
<body>
<div id="window-mmask" class="window-mmask" ><div class="window-mmask-msg" >页面加载中,请稍候...</div></div>
<div class="easyui-layout" data-options="fit:true"  >
	<div data-options="region:'center'" >
		<table id="gridList" ></table>
	</div>
</div>
<div id="searchbar">
<div class="maction">
	    <a href="javascript:void(0)" id="picker" class="easyui-linkbutton" data-options="plain:true,iconCls:'mbutton'">导入模板设置表头</a>
	<a href="javascript:void(0)" id="saveButton" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-save mbutton'">保存</a>
</div>
<form id="searchForm" >
	excel报表<input id="search-excelConfigId" name="excelConfigId" class="easyui-textbox" style="width:200px;">
	<a id="searchButton" href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-search mbutton'" >查询</a>
</form>
</div>
<script src="/static/basic/plugins/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="/static/basic/plugins/easyui/jquery.easyui.min.js"></script>
<script src="/static/init.js"></script>
<script src="/static/basic/plugins/easyui/locale/easyui-lang-zh_CN.js"></script>	
<script src="/static/searchEdit.js"></script>
<script type="text/javascript" src="/static/basic/plugins/webuploader/webuploader.html5only.min.js"></script>
<!-- <script src="/static/basic/plugins/moment/moment.min.js"></script> -->
<script>
var options={
	idField:"Id",
	page_id:"395498163712557056",
	grid_id:"gridList",
	sqlname:"grid_395498163712557056_excel_data_header",
	showCheckbox:false,
	rownumbers:false,
};
var editRow=null;
var excelConfigId=null;
function initWebUploader(pickfiles){
	//初始化Web Uploader
	var uploader = WebUploader.create({

		// 选完文件后，是否自动上传。
		auto: true,
		// 文件接收服务端。
		server: '/compare/setExcelHeader',

		// 选择文件的按钮。可选。
		// 内部根据当前运行是创建，可能是input元素，也可能是flash.
		pick: {
			id: "#picker"
		},
		formData: {
			//excelConfigId:excelConfigId //非固定值不起作用
		},
		// 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
		resize: false,
		accept: {
			title: 'excel',
			extensions: 'xlsx',//没有测试是否起作用
		    mimeTypes:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'//这个起作用
		}
	});
	uploader.on('beforeFileQueued', function(file) {
		console.log("beforeFileQueued");
		console.log(file);
		if (file.ext !== 'xlsx') {
			// 文件格式不符合要求，进行处理
			return false;
		}
		console.log(excelConfigId)
		console.log(uploader.options.formData)
		uploader.options.formData={excelConfigId:excelConfigId};
		console.log(uploader.options.formData)
	});
	// 当有文件被添加进队列的时候
	uploader.on( 'fileQueued', function( file ) {
		console.log("fileQueued");
		console.log(file);
		if (file.ext !== 'xlsx') {
			// 文件格式不符合要求，进行处理
			uploader.removeFile(file, true);
			return false;
		}

	});

	// 文件上传过程中创建进度条实时显示。
	// uploader.on( 'uploadProgress', function( file, percentage ) {
	// 	var $li = $( '#'+file.id ),
	// 			$percent = $li.find('.progress .progress-bar');
	//
	// 	// 避免重复创建
	// 	if ( !$percent.length ) {
	// 		$percent = $('<div class="progress progress-striped active">' +
	// 				'<div class="progress-bar" role="progressbar" style="width: 0%">' +
	// 				'</div>' +
	// 				'</div>').appendTo( $li ).find('.progress-bar');
	// 	}
	//
	// 	$li.find('p.state').text('上传中');
	//
	// 	$percent.css( 'width', percentage * 100 + '%' );
	// });

	uploader.on( 'uploadSuccess', function( file,response ) {
		console.log(response);
		if(!response.success){
			$.messager.alert("提示", "设置表头失败！<br>"+response.message);
		}else{
			info("设置成功");
		}
		//$("#cert_id").val(response.data[0].item_id);
		//$( '#'+file.id ).find('p.state').text('已上传');
	});

	uploader.on( 'uploadError', function( file ) {
		console.log(file);
		//$( '#'+file.id ).find('p.state').text('上传出错');
	});

	uploader.on( 'uploadComplete', function( file ) {
		//$( '#'+file.id ).find('.progress').fadeOut();
		uploader.reset();
		grid1.load();
	});

	uploader.on('uploadBeforeSend', function(object,data,headers) {
		let access_token=localStorage.getItem("access_token");
		headers.Accept="application/json";
		//headers["X-Requested-With"]="XMLHttpRequest";
		headers.Authorization="Local "+access_token;
		//headers.Authorization=BearerType+access_token;
	});
}
//document ready
$(function(){
	grid1=new uniontrip.datagrid.searchEdit(options);
	//tab
	$('#addButton').bind('click', function(){
		editRow=null;
		EditDialog.dialog('setTitle','新增').dialog('open');
	});//新增行绑定事件
	$('#editButton').bind('click',function(){
		gridSelectRow(grid1.grid,function(row){
			editRow=row;
			EditDialog.dialog('setTitle','修改').dialog('open');
		});
	});//编辑按钮绑定事件
	$('#removeButton').bind('click',function(){gridConfirm(grid1.grid,"是否确认删除？",deleteSubmit);});//删除行绑定事件
	
	$('#searchButton').bind('click',grid1.load);//查询操作绑定事件
	//row
	$('#addRowButton').bind('click', addRow);//新增行绑定事件
	//$('#editButton').bind('click',grid1.editRow);//编辑按钮绑定事件
	//$('#removeButton').bind('click',grid1.remove);//删除行绑定事件
	$('#saveButton').bind('click', grid1.accept);//保存datagrid绑定事件
	$('#setBtn1').bind('click',setColumnTitle);//取消行编辑绑定事件
	//$('#endEditingButton').bind('click', grid1.endEditing);//结束行编辑绑定事件
	//$('#rejectButton').bind('click', grid1.rejectChanges);//撤销编辑绑定事件		
    //$('#showChangesButton').bind('click',grid1.showChanges);//信息变更提示

    //初始化搜索框
    initsearch();
	initWebUploader();
});

function save(){
	if($("#form-1").form('enableValidation').form('validate')){
		$.post("/authenticated/gas/station/save",$("#form-1").serializeObject(),function(result){
			if(result.success){
				info( "保存成功！");
				EditDialog.dialog('close');
				grid1.load();
			}else{
				$.messager.alert("提示", "保存失败！<br>"+result.message);
			}
		});
	}
}
/**
 * 新增一行
 */
function addRow(){
//ROW
	var newRowData={id:guid()};//初始化行数据,creator_id:sessionuser.id,creator_name:sessionuser.nickname,create_time:moment().format('YYYY-MM-DD')
	grid1.grid.datagrid('append',newRowData);	
	//tab
	//parent.addTab("新增","/view/form.html");
}
function onUpdate(){
	var selectRows=grid1.grid.datagrid('getSelections');
	if(selectRows.length>0){
		parent.addTab("编辑["+selectRows[0].id+"]","/view/form.html?id="+selectRows[0].id);
	}else{
		$.messager.alert("提示", "请选择一条数据！");
	}
}
function setColumnTitle(){
	var selectRows=grid1.grid.datagrid('getSelections');
	if(selectRows.length>0){
		$.post("/compare/setColumnTitle",{},function(result){
			if(result.success){
				$.messager.alert("提示", "设置成功！");
			}else{
				$.messager.alert("提示", "设置失败！<br>"+result.message);
			}
		});
	}else{
		$.messager.alert("提示", "请选择一条数据！");
	}
}
/**
 * 删除提交
 * @param selectRow
 */
function deleteSubmit(selectRow){
	$.post("/authenticated/ajax/updates/grid_395498163712557056_excel_data_header-delete",JSON.stringify({ids:[selectRow.id]}),function(result){
		if(result.success){
			$.messager.alert("提示", "删除成功！");
			grid1.load();
		}
	});
}

function onDeleteRow(){
	gridConfirm(grid1.grid,"是否确认删除",deleteRow);
}
function deleteRow(row){
	$.post("/authenticated/xx/delete",JSON.stringify({"id":row.id}),function(result){
		if(result.success){
			$.messager.alert("提示", "删除成功！");
			grid1.load();
		}else{
			$.messager.alert("提示", "删除失败！<br>"+result.message);
		}
	});
}


/**
 * 初始化搜索条件
 */
function initsearch(){
	/*$('#search-product_class_id').combotree({
		panelHeight:'auto',
		icons: [{
			iconCls:'icon-clear',
			handler:function(){
				$('#search-product_class_id').combotree('clear');
			}
		}],
	    data:grid1.combotreeDatas.product_class_id
	});
	*/
	$('#search-excelConfigId').combobox({
		panelHeight:'auto',
		editable:false,
		valueField:'codeValue',
		textField:'codeName',
		onSelect:function (record){
			//console.log(record);
			excelConfigId=record.codeValue;
			console.log(excelConfigId)
		},
		icons: [{
			iconCls:'icon-clear',
			handler:function(e){
				$(e.data.target).combobox('clear');
			}
		}],
		data:grid1.comboboxDatas.excelConfigId
	});
}
</script>
</body>
</html>